#include "ObjectManager.h"

namespace GamePlayScene
{
	ObjectManager::ObjectManager()
	{
		_objects.clear();
	}

	void ObjectManager::addFront(ObjectPtr object)
	{
		_objects.push_front(object);
	}

	void ObjectManager::addBack(ObjectPtr object)
	{
		_objects.push_back(object);
	}

	void ObjectManager::myTurnUpdate( float frameTimer )
	{
		each([&](ObjectPtr object){ object->myTurnUpdate(frameTimer);});
	}

	void ObjectManager::update( float frameTimer )
	{
		each([&](ObjectPtr object){ object->update(frameTimer); }); 
	}

	void ObjectManager::draw()
	{
		each([](ObjectPtr object){ object->draw(); });
	}

	void ObjectManager::finish()
	{
		each([](ObjectPtr object){ object->finish();});
		_objects.clear();
	}

	void ObjectManager::remove()
	{
		_objects.remove_if([](ObjectPtr object){ 
			if (!object->isActive()) {
				object->finish();
				return true;
			}
			return false;
		});
	}

	void ObjectManager::debugUpdate(float frameTimer)
	{
		each([&](ObjectPtr object){ object->debugUpdate(frameTimer); });
	}

	void ObjectManager::debugDraw()
	{
		each([](ObjectPtr object){ object->debugDraw(); });
	}

	void ObjectManager::each(std::function<void (ObjectPtr)> f)
	{
		std::for_each(_objects.begin(), _objects.end(), f);
	}
};